package cn.swufe.edu.stu.hw_tools.Time;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import cn.swufe.edu.stu.hw_tools.R;

public class TimerActivity extends AppCompatActivity {
    private TextView timerTextView ;
    private Button startButton, stopButton, resetButton,backButton;
    private long timeElapsed = 0;
    private boolean isRunning = false;
    private Handler handler = new Handler();
    private boolean isPaused = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timer);

        timerTextView = findViewById(R.id.timerTextView);
        startButton = findViewById(R.id.startButton);
        stopButton = findViewById(R.id.stopButton);
        resetButton = findViewById(R.id.resetButton);
        backButton = findViewById(R.id.backButton);

        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startTimer();
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopTimer();
            }
        });

        resetButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                resetTimer();
            }
        });

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                setResult(2,intent);
                finish();
            }
        });
    }

    private void startTimer() {
        if (!isRunning) {
            if(!isPaused){
                timeElapsed = 0;
                isRunning = true;
                handler.postDelayed(runnable, 100);
            }else{
                isRunning = true;
                handler.postDelayed(runnable, 100);
            }
        }
    }

    private void stopTimer() {
        isRunning = false;
        isPaused = true;
        handler.removeCallbacks(runnable);
    }

    private void resetTimer() {
        stopTimer();
        timeElapsed = 0;
        updateTimerText();
    }

    private void updateTimerText() {
        timerTextView.setText(formatTime(timeElapsed));
    }

    private String formatTime(long millis) {
        long seconds = (millis / 10) % 60;
        long minutes = (millis / (10 * 60)) % 60;
        long hours = millis / (10 * 60 * 60);
        long mis = millis % 10;
        return String.format("%02d:%02d:%02d:%d", hours, minutes, seconds ,mis);
    }

    private final Runnable runnable = new Runnable() {
        @Override
        public void run() {
            timeElapsed += 1;
            updateTimerText();
            if (isRunning) {
                handler.postDelayed(this, 100);
            }
        }
    };
}